﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace OpenRaiden.Tools
{
    static class TileFont
    {
        static SpriteBatch spriteBatch;
        static ContentManager content;
        static Texture2D fontSheet;
        static Color[] transparencyKey;

        public static void Initialize(SpriteBatch s, ContentManager c)
        {
            spriteBatch = s;
            content = c;
            fontSheet = content.Load<Texture2D>("raiden_font_spritesheet");
            transparencyKey = new Color[fontSheet.Width * fontSheet.Height];
            fontSheet.GetData(transparencyKey);
        }

        public static void DrawString(Vector2 p, string s)
        {
            string[] sray = s.Split('\n');
            List<char[]>clist = new List<char[]>();

            foreach (string ss in sray)
                clist.Add(ss.ToCharArray());

            /*
            foreach (char[] cArray in clist)
            {
                for (int i = 0; i < cray.Length; i++)
                {
                    spriteBatch.Draw(
                        fontSheet,
                        new Vector2(p.X + (8 * i), p.Y + (8 * line)),
                        new Rectangle(
                            (int)GetCharPos(cray[i]).X,
                            (int)GetCharPos(cray[i]).Y,
                            8,
                            8),
                        Color.White);
                }
            }
            */

            for (int i = 0; i < clist.Count; i++)
            {
                for (int j = 0; j < clist[i].Length; j++)
                {
                    spriteBatch.Draw(
                        fontSheet,
                        new Vector2(p.X + (j * 8), p.Y + (i * 8)),
                        new Rectangle( (int)GetCharPos((clist[i])[j]).X, (int)GetCharPos((clist[i])[j]).Y, 8, 8),
                        Color.White);
                }
            }

            
        }

        public static Vector2 GetCharPos(char c)
        {
            if (31 < c && c < 48) // ' ' - '/'
            {
                return new Vector2((c - 32) * 9, 0);
            }
            else if (47 < c && c < 64) // '0' - '?'
            {
                return new Vector2((c - 48) * 9, 9);
            }
            else if (c == 64) // '@'
            {
                return new Vector2((c - 64) * 9, 18);
            }
            else if (64 < c && c < 80) // 'A' - 'O'
            {
                return new Vector2((c - 64) * 9, 18);
            }
            else if(79 < c && c < 91) // 'P - 'Z'
            {
                return new Vector2((c - 80) * 9, 27); 
            }
            else if (96 < c && c < 112) // 'a' - 'o'
            {
                return new Vector2((c - 96) * 9, 18);
            }
            else if (111 < c && c < 123) // 'p' - 'z'
            {
                return new Vector2((c - 112) * 9, 27);
            }
            else //return blank character if not found
            {
                return new Vector2(0, 0);
            }
        }

    }
}
